<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/1 15:00
 * Modified Time: 2019/7/1 15:00
 * Version: 2.0
 */

namespace App\Modules\MemberCard\Criterias;


use Flower\Core\Repository\Contracts\CriteriaInterface;
use Flower\Core\Repository\Contracts\RepositoryInterface;
use function foo\func;

class GetNotAuditedMemberCardCriteria implements CriteriaInterface
{
    private $store_id;
    private $user_id;
    private $status;
    private $keywords;

    public function __construct($user_id, $store_id, $status, $keywords)
    {
        $this->store_id = $store_id;
        $this->user_id = $user_id;
        $this->status = $status;
        $this->keywords = $keywords;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $store_id = $this->store_id;
        $user_id = $this->user_id;
        $status = $this->status;
        $keywords = $this->keywords;


        //查询
        $select = [
            'hx_member_card.id',
            'hx_member_card.name',
            'hx_member_card.member_id',
            'hx_member_card.mobile',
            'hx_member_card.sex',
            'hx_member_card.type_id',
            'hx_member_card.status',
            'hx_member_card.balance',
            'hx_member_card.count',
            'hx_member_card.type_id',
            'hx_member_card.attribute',
            'hx_member_card.user_id',
            'hx_member_card.source',
            'hx_member_card.card_num',
            'hx_member_card.created_at as created_time'
        ];

        $select = implode($select, ',');


        $model = $model->selectRaw($select)
            ->with(['member' => function ($query) {
                $select = [
                    'hx_member.id',
                    'hx_member.name',
                    'hx_picture.image as face'
                ];

                $select = implode($select, ',');
                $query->selectRaw($select)
                    ->leftJoin('picture', 'face', 'picture.id');
            }])
            ->with(['cardType' => function ($query) {
                $select = [
                    'hx_card_type.id',
                    'hx_card_type.name',
                    'hx_card_type.store_id',
                    'hx_card_type.category',
                    'hx_card_type.price',
                    'hx_card_type.count',
                    'hx_card_type.present',
                    'hx_card_type.skin',
                    'hx_card_type.royalty',
                    'hx_card_type.status',
                    'hx_card_type.discount_way',
                    'hx_card_type.created_at',
                ];

                $select = implode($select, ',');
                $query->selectRaw($select);
            }])
            ->when($keywords, function ($query) use ($keywords) {
                $query->where(function ($query) use ($keywords) {
                    $query->where('member_card.name', 'like', '%' . $keywords . '%')
                        ->orWhere('member_card.mobile', 'like', '%' . $keywords . '%');
                });
            })
            ->when($status!==null, function ($query) use ($status) {
                $query->where('member_card.status', $status);
            })
            ->with(['royalty'=>function($query){
                $query->select('id','item_id','amount');
            }])
            ->where('member_card.user_id', $user_id)
            ->where('member_card.store_id', $store_id)
            ->where(function ($query) {
                $query->where('member_card.source', 1)
                    ->orWhere('member_card.source', 4);
            })
            ->orderBy('status', 'ASC')
            ->orderBy('id', 'DESC');


        return $model;
    }
}